package com.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mail.common.DataResult;
import com.mail.dto.EmailTeamDto;
import com.mail.dto.TeamMemberDto;
import com.mail.enums.ResponseCode;
import com.mail.mapper.EmailTeamMapper;
import com.mail.mapper.TeamMemberMapper;
import com.mail.mapper.UserMapper;
import com.mail.pojo.EmailTeam;
import com.mail.pojo.TeamMember;
import com.mail.pojo.User;
import com.mail.service.EmailTeamService;
import com.mail.vo.EmailTeamVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EmailTeamServiceImpl extends ServiceImpl<EmailTeamMapper, EmailTeam> implements EmailTeamService {

    @Autowired
    private EmailTeamMapper emailTeamMapper;

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public DataResult updateTeam(Map<String, Object> map) {
        long id = Long.parseLong((String) map.get("id"));
        String teamManager=(String) map.get("teamManager");
        String teamName=(String) map.get("teamName");
        String teamNotes=(String) map.get("teamNotes");
        UpdateWrapper<EmailTeam> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set(StringUtils.isNotBlank(teamName), "team_name", teamName);
        wrapper.set(StringUtils.isNotBlank(teamNotes), "team_notes", teamNotes);
        if(StringUtils.isNotBlank(teamManager)){
            long teamManager1 = Long.parseLong(teamManager);
            wrapper.set("team_manager", teamManager1);
        }
        emailTeamMapper.update(null,wrapper);
        return DataResult.success("操作成功！");
    }

    @Override
    @Transactional
    public DataResult addTeam(EmailTeamDto emailTeamDto) {
        String teamName=emailTeamDto.getTeamName();
        Long teamManager=emailTeamDto.getTeamManager();
        String teamNotes=emailTeamDto.getTeamNotes();
        if(!StringUtils.isNotBlank(teamName)||!StringUtils.isNotBlank(teamNotes)||teamManager==null){
            return DataResult.error(ResponseCode.LONG_INTEGER_IS_NULL.getMsg());
        }
        EmailTeam emailTeam=new EmailTeam(teamName,teamManager,teamNotes);
        emailTeamMapper.insert(emailTeam);
        TeamMember teamMember=new TeamMember(emailTeam.getId(),teamManager,"1");
        teamMemberMapper.insert(teamMember);
        return DataResult.success(ResponseCode.SUCCESS.getMsg());
    }

    @Override
    public DataResult queryTeamPage(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        long id = Long.parseLong((String) map.get("id"));
        List<EmailTeam> list=emailTeamMapper.queryTeamByMember(id,(page-1)*limit,limit);
        return DataResult.success(list);
    }

    @Override
    @Transactional
    public DataResult deleteTeamById(long id) {
        emailTeamMapper.deleteById(id);
        teamMemberMapper.deleteTeamMemberByTeam(id);
        return DataResult.success(ResponseCode.SUCCESS.getMsg());
    }

    @Override
    @Transactional
    public DataResult deleteMemberById(long id) {
        teamMemberMapper.deleteById(id);
        return DataResult.success(ResponseCode.SUCCESS.getMsg());
    }

    @Override
    @Transactional
    public DataResult addMember(TeamMemberDto teamMemberDto) {
        Long teamId=teamMemberDto.getTeamId();
        String[] memberEmail=teamMemberDto.getMemberEmail();
        if(memberEmail.length<1||teamId==null){
            return DataResult.error(ResponseCode.LONG_INTEGER_IS_NULL.getMsg());
        }
        for(int i=0;i<memberEmail.length;i++){
            Long userId=userMapper.queryIdByUserName(memberEmail[i]);
            if(userId==null){
                return DataResult.error("平台不存在此用户："+memberEmail[i]);
            }
            if(teamMemberMapper.queryIsNull(teamId,userId)!=0){
                return DataResult.error("团队已添加过此用户："+memberEmail[i]);
            }
            teamMemberMapper.insert(new TeamMember(teamId,userId));
        }
        return DataResult.success(ResponseCode.SUCCESS.getMsg());
    }

    @Override
    public DataResult queryMemberPage(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        long id = Long.parseLong((String) map.get("id"));
        Map<String,Object>map1=new HashMap<>();
        map1.put("records",userMapper.queryUserByTeamId(id,(page-1)*limit,limit));
        map1.put("total",userMapper.queryUserByTeamIdCount(id));
        return DataResult.success(map1);
    }

    @Override
    public DataResult queryTeamById(long id) {
        EmailTeam emailTeam=emailTeamMapper.selectById(id);
        User user=userMapper.selectById(emailTeam.getTeamManager());
        EmailTeamVo emailTeamVo=new EmailTeamVo(emailTeam.getId(),emailTeam.getTeamName(),emailTeam.getTeamManager(),emailTeam.getTeamNotes(),emailTeam.getCreateTime(),emailTeam.getUpdateTime(),emailTeam.getCreateUser(),emailTeam.getUpdateUser(),user.getNickname(),user.getUsername());
        return DataResult.success(emailTeamVo);
    }
}
